$(document).ready(function () {
	$(function() {
	    var startDate;
	    var endDate;
	    
	    var selectCurrentMonth = function() {
	        window.setTimeout(function () {
	            $('.month-picker').find('.ui-datepicker-current-day a').addClass('ui-state-active');
	        }, 1);
	    };
	    function getDaysInMonth(m, y) {
    	   return /8|3|5|10/.test(--m)?30:m==1?(!(y%4)&&y%100)||!(y%400)?29:28:31;
    	}
	    $('.month-picker').datepicker( {
	        showOtherMonths: true,
	        selectOtherMonths: true,
	        onSelect: function(dateText, inst) { 
	            var date = $(this).datepicker('getDate');
	            startDate = new Date(date.getFullYear(), date.getMonth(), 1);          
	            endDate = new Date(date.getFullYear(), date.getMonth(), getDaysInMonth(date.getMonth() + 1, date.getFullYear()));
	            var dateFormat = "yy-mm-dd";
	            $(this).parent().find('#startDate').val($.datepicker.formatDate( dateFormat, startDate, inst.settings ));
	            $(this).parent().find('#endDate').val($.datepicker.formatDate( dateFormat, endDate, inst.settings ));
	            
	            selectCurrentMonth();
	        },
	        beforeShowDay: function(date) {
	            var cssClass = '';
	            if(date >= startDate && date <= endDate)
	                cssClass = 'ui-datepicker-current-day';
	            return [true, cssClass];
	        },
	        onChangeMonthYear: function(year, month, inst) {
	            selectCurrentMonth();
	        }
	    });
	    
	    $('.month-picker .ui-datepicker-calendar tr').live('mousemove', function() { $(this).find('td a').addClass('ui-state-hover'); });
	    $('.month-picker .ui-datepicker-calendar tr').live('mouseleave', function() { $(this).find('td a').removeClass('ui-state-hover'); });
	});
	
	$(function() {
	    var startDate;
	    var endDate;
	    
	    var selectCurrentWeek = function() {
	        window.setTimeout(function () {
	            $('.week-picker').find('.ui-datepicker-current-day a').addClass('ui-state-active');
	        }, 1);
	    };
	    $('.week-picker').datepicker( {
	        showOtherMonths: true,
	        selectOtherMonths: true,
	        onSelect: function(dateText, inst) { 
	            var date = $(this).datepicker('getDate');
	            startDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay());          
	            endDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay() + 6);
	            var dateFormat = "yy-mm-dd";
	            $(this).parent().find('#startDate').val($.datepicker.formatDate( dateFormat, startDate, inst.settings ));
	            $(this).parent().find('#endDate').val($.datepicker.formatDate( dateFormat, endDate, inst.settings ));
	            
	            selectCurrentWeek();
	        },
	        beforeShowDay: function(date) {
	            var cssClass = '';
	            if(date >= startDate && date <= endDate)
	                cssClass = 'ui-datepicker-current-day';
	            return [true, cssClass];
	        },
	        onChangeMonthYear: function(year, month, inst) {
	            selectCurrentWeek();
	        }
	    });
	    
	    $('.week-picker .ui-datepicker-calendar tr').live('mousemove', function() { $(this).find('td a').addClass('ui-state-hover'); });
	    $('.week-picker .ui-datepicker-calendar tr').live('mouseleave', function() { $(this).find('td a').removeClass('ui-state-hover'); });
	});
	$(".week-picker, .month-picker, .year-picker, .DateInfo").hide();
	$(".statistic_week").live("click", function(){
		$(this).parent().find(".week-picker, .DateInfo").show();
		$(this).parent().find(".month-picker, .year-picker").hide();
	});
	$(".statistic_month").live("click", function(){
		$(this).parent().find(".month-picker, .DateInfo").show();
		$(this).parent().find(".week-picker, .year-picker").hide();
	});
	$(".statistic_year").live("click", function(){
		$(this).parent().find(".year-picker").show();
		$(this).parent().find(".week-picker, .month-picker, .DateInfo").hide();
	});
	$("table.chart-line").each(function() {
        var colors = [];
        $("table.chart-line thead th:not(:first)").each(function() {
            colors.push($(this).css("color"));
        });
        $(this).graphTable({
            series: 'columns', position: 'replace',width : '99%',height: '350px', colors: colors
        }, { xaxis: {  tickSize: 3 },
		series: {
				points: {show: true },
                lines: { show: true, fill: false, steps: false },
                bars: { show: false, barWidth: 0.6 }
			}
        });
    });	
	
	function showTooltip(x, y, contents) {
        $('<div id="tooltip" >' + contents + '</div>').css({
            position: 'absolute',
            display: 'none',
            top: y -13,
            left: x + 10
        }).appendTo("body").show();
    }

    var previousPoint = null;
    $(".chart_flot").bind("plothover", function(event, pos, item) {
												
        $("#x").text(pos.x);
        $("#y").text(pos.y);

        if (item) {
            if (previousPoint != item.dataIndex) {
                previousPoint = item.dataIndex;

			$(this).attr('title',item.series.label);
			$(this).trigger('click');
                $("#tooltip").remove();
                var x = item.datapoint[0],
                    y = item.datapoint[1];

                showTooltip(item.pageX, item.pageY, "<b>" + item.series.label + "</b> : " + y);
            }
        }  else {
            $("#tooltip").remove();
            previousPoint = null;
        }
    });
	
	$("#btnStatisticPYear").live("click", function(){
		$("#formStatisticP").find("#startDate").val("");
		$("#formStatisticP").find("#endDate").val("");
		$("#formStatisticP").submit();
	});
	
	$("#btnStatisticP").live("click", function(){
		$("#formStatisticP").find("#purchasedYear").val("");
		$("#formStatisticP").submit();
	});
	
	$("#btnStatisticYear").live("click", function(){
		$("#formStatistic").find("#startDate").val("");
		$("#formStatistic").find("#endDate").val("");
		$("#formStatistic").submit();
	});
	
	$("#btnStatistic").live("click", function(){
		$("#formStatistic").find("#purchasedYear").val("");
		$("#formStatistic").submit();
	});
});